<?php
namespace Addons\MinProgramOrder\Controller\H5;

use Models\BaseModel as Model;
use Session;
use Addons\OrderManager\Helper;
use Addons\QyWechat\Services\QyWechatApi;
use Addons\QyWechat\Services\JsSdk;
use Addons\QyWechat\Helper as QyHelper;

class Order extends \BaseController
{
	public function index(){
		$corp_id = \Request::get('corp_id');
		$agent_id = \Request::get('agent_id');
		$qyConfig = (new Model('qy_wechat_config'))->find(['corp_id'=>$corp_id]);
		$wid = $qyConfig->wid; 
		// $replyConfig = (new Model('qy_reply_config'))->find(['wid'=>$wid,'agent_id'=>$agent_id]);
		$replyConfig = QyHelper::getInstance()->get_message_send_config($wid,$agent_id);
		if($_GET['code']){
			$code = $_GET['code'];
			$app = QyWechatApi::getInstance()->get_qy_agent_app($corp_id,$replyConfig->agent_id,$replyConfig->secret);
			$accessToken = $app->access_token;
			$token = $accessToken->getToken()['access_token']; // token 数组  token['access_token'] 字符串
			$url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='.$token.'&code='.$code;
			$resArr = json_decode(curlGet($url),1);
			$jsSdkServe = new JsSdk($corp_id,$replyConfig->agent_id,$replyConfig->secret);
			$wxConfig = $jsSdkServe->getSignPackage();
			$agentConfig = $jsSdkServe->getSignPackage('','agent');
			$qyUser = (new Model('qy_user_list'))->find(['wid'=>$wid,'userid'=>$resArr['UserId']]);
			$this->assign("corp_id", $corp_id);
			$this->assign("agent_id", $replyConfig->agent_id);
			$this->assign("wxConfig", $wxConfig);		
			$this->assign("agentConfig", $agentConfig);
			$this->assign("wid", $wid);
			$this->assign("token", $token);
			$this->assign("userId", $qyUser->id);
			return $this->show();
		}else{
			$redirect_uri = urlencode(Helper::getInstance()->get_http_server_name().'/Addons/MinProgramOrder/H5/Order.html?corp_id='.$corp_id.'&agent_id='.$replyConfig->agent_id);
			$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$corp_id.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
			echo "<script>window.location = '{$url}'</script>";
		}
	}

	public function order_list(){
		$etUserId = \Request::get('etUserId');
		$userId = \Request::get('userId');
		$token = \Request::get('token');
		$corp_id = \Request::get('corp_id');
		$agent_id = \Request::get('agent_id');
		$qyConfig = (new Model('qy_wechat_config'))->find(['corp_id'=>$corp_id]);
		$wid = $qyConfig->wid; 
		$replyConfig = QyHelper::getInstance()->get_message_send_config($wid,$agent_id);
		if(!$userId){
			if($_GET['code']){
				$code = $_GET['code'];
				$app = QyWechatApi::getInstance()->get_qy_agent_app($corp_id,$replyConfig->agent_id,$replyConfig->secret);
				$accessToken = $app->access_token;
				$token = $accessToken->getToken()['access_token']; // token 数组  token['access_token'] 字符串
				$url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='.$token.'&code='.$code;
				$resArr = json_decode(curlGet($url),1);
				$qyUser = (new Model('qy_user_list'))->find(['wid'=>$wid,'userid'=>$resArr['UserId']]);
				$userId = $qyUser->id;
			}else{
				$redirect_uri = urlencode(Helper::getInstance()->get_http_server_name().'/Addons/MinProgramOrder/H5/Order.html?corp_id='.$corp_id.'&agent_id='.$replyConfig->agent_id."&etUserId=".$etUserId."&token=".$token);
				$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$corp_id.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
				echo "<script>window.location = '{$url}'</script>";
			}
		}
		$copyright = QyHelper::copyright($wid);
		$url = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token='.$token.'&external_userid='.$etUserId;
		$resArr = json_decode(curlGet($url),1);
		$et = (new Model('qy_externalcontact_list'))->find(['wid'=>$wid,'external_userid'=>$etUserId,'user_id'=>$userId]);
		if(!$et->id){
			die('未知客户');
		}
		$order_list = (new Model('qy_applet_order'))->where(['wid'=>$wid,'UnionID'=>$resArr["external_contact"]["unionid"]])->list_all();
		$h = QyHelper::getInstance();
		$this->assign('h',$h);
		$this->assign('order_list',$order_list);
		$this->assign('copyright',$copyright);
		return $this->show();
	}
}
